package com.gr.dmbackend.service.Impl.student;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gr.dmbackend.mapper.RepairMapper;
import com.gr.dmbackend.mapper.StudentMapper;
import com.gr.dmbackend.model.Repair;
import com.gr.dmbackend.model.Student;
import com.gr.dmbackend.service.student.GetRevokedRepairService;
import com.gr.dmbackend.utils.GetUserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class GetRevokedRepairServiceImpl implements GetRevokedRepairService {

    @Autowired
    private StudentMapper studentMapper;

    @Autowired
    private RepairMapper repairMapper;

    @Override
    public JSONObject getRevokedRepair(int page) {

        JSONObject resp = new JSONObject();

        QueryWrapper<Student> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("student_id", GetUserUtil.getCurrentUser().getUsername());
        Student student = studentMapper.selectOne(queryWrapper1);
        if (student == null) {
            resp.put("error_message", "当前学生不存在");
            return resp;
        }

        IPage<Repair> repairIPage = new Page<>(page, 10);
        QueryWrapper<Repair> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id")
                .eq("student_id", student.getStudentId())
                .eq("repair_state", "已撤销");
        List<Repair> records = repairMapper.selectPage(repairIPage, queryWrapper).getRecords();
        resp.put("records", records);
        resp.put("records_count", repairMapper.selectCount(queryWrapper));
        resp.put("error_message", "success");

        return resp;
    }
}
